package com.charles.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.charles.config.ExportProperties;
import com.charles.constants.EasyExcelConstants;
import com.charles.context.ExportContext;
import com.charles.entity.AppExportRecord;
import com.charles.enums.ExportRecordStatus;
import com.charles.event.ExportEvent;
import com.charles.excel.CustomSheetWriteHandler;
import com.charles.mapper.AppExcelRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Date;

/**
 * @author charles
 * @date 2023/5/15 0:41
 */
@Slf4j
public class DefaultExportRecordHandler implements ExportRecordHandler {

    @Autowired
    private AppExcelRecordMapper excelRecordMapper;

    @Autowired
    private ExportProperties exportProperties;

    @Override
    public void preHandle(ExportContext exportContext) {
        AppExportRecord exportRecord = new AppExportRecord();
        ExportEvent exportEvent = exportContext.getAttribute("exportEvent");
        String fileName = exportEvent.getFileName();
        fileName = fileName + "_" + DateUtil.format(new Date(), "yyyyMMdd") + EasyExcelConstants.FILE_TYPE;
        exportContext.getAttributes().put("fileName", fileName);
        exportRecord.setRecordStatus(ExportRecordStatus.START.getCode()).setCreateBy(-1).setCreateTime(new Date()).setModule(exportProperties.getModuleName()).setFileName(fileName);
        excelRecordMapper.insert(exportRecord);
        exportContext.getAttributes().put("record", exportRecord);
    }

    @Override
    public void postHandle(ExportContext exportContext) {
        AppExportRecord exportRecord = exportContext.getAttribute("record");
        exportRecord.setRecordStatus(ExportRecordStatus.END.getCode()).setResultInfo("处理成功").setUpdateTime(new Date()).setFileUrl(exportContext.getAttribute("fileUrl"));
        excelRecordMapper.updateById(exportRecord);
    }

    @Override
    public void doHandle(ExportContext exportContext) {
        ExportEvent exportEvent = exportContext.getAttribute("exportEvent");
        String fileName = exportContext.getAttribute("fileName");
        String fullPath = "";
        String osName = System.getProperty("os.name");
        if (osName.contains("Linux")) {
            fullPath = exportProperties.getUploadPath() + fileName;
        } else {
            fullPath = System.getProperty("user.dir") + File.separator + fileName;
        }
        exportContext.getAttributes().put("fileUrl", fullPath);
        log.info("文件保存路径：{}, 系统名称：{}", fullPath, osName);
        //写入文件
        EasyExcel.write(new File(fullPath), exportEvent.getAClass()).sheet(exportEvent.getSheetName()).registerWriteHandler(new CustomSheetWriteHandler()).doWrite(exportEvent.getData());

    }

    @Override
    public void handleException(ExportContext exportContext, Exception e) {
        AppExportRecord exportRecord = exportContext.getAttribute("record");
        exportRecord.setRecordStatus(ExportRecordStatus.END.getCode()).setResultInfo("处理失败: " + e.getMessage()).setUpdateTime(new Date());
        excelRecordMapper.updateById(exportRecord);
    }
}
